package com.esun.payment_api.service.impl;

import com.esun.common.bean.PageRequest;
import com.esun.common.utils.BlankUtils;
import com.esun.payment_api.bean.InvTaskDetailBean;
import com.esun.payment_api.domain.InvFormTask;
import com.esun.payment_api.domain.InvFormTaskExample;
import com.esun.payment_api.domain.SysDictContract;
import com.esun.payment_api.dto.PInvInsParam;
import com.esun.payment_api.mapper.InvFormTaskMapper;
import com.esun.payment_api.service.InvFormTaskService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class InvFormTaskServiceImpl implements InvFormTaskService {

    @Resource
    private InvFormTaskMapper invFormTaskMapper;

    @Override
    public int insert(InvFormTask record){
        return invFormTaskMapper.insertSelective(record);
    }

    @Override
    public int deleteById(Integer id) {
        return invFormTaskMapper.deleteById(id);
    }

    @Override
    public int delete(List<Integer> ids){
        InvFormTask record = new InvFormTask();
        record.setIsDeleted("1");
        InvFormTaskExample example = new InvFormTaskExample();
        example.createCriteria().andIsDeletedEqualTo("0").andIdIn(ids);
        return invFormTaskMapper.updateByExampleSelective(record,example);
    }

    @Override
    public int update(InvFormTask record){
        return invFormTaskMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public InvTaskDetailBean getById(Integer id){
        return invFormTaskMapper.getById(id);
    }

    @Override
    public List<InvFormTask> getListByInvId(Integer invId){
        InvFormTask record = new InvFormTask();
        InvFormTaskExample example = new InvFormTaskExample();
        example.createCriteria().andInvIdEqualTo(invId).andIsDeletedEqualTo("0");
        return invFormTaskMapper.selectByExample(example);
    }

    @Override
    public void insertMultiple(PInvInsParam param) {
        invFormTaskMapper.pInvInsTask(param);
    }

    @Override
    public List<SysDictContract> getSelectContractList(Short coId, Integer invId, String searchStr, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        PageRequest pageRequest = new PageRequest();
        Map<String, String> conditions = new HashMap<>();
        conditions.put("coId", coId.toString());
        conditions.put("invId", invId.toString());
        if (!BlankUtils.isBlank(searchStr)) {
            conditions.put("searchStr", searchStr);
        }
        pageRequest.setConditions(conditions);
        return invFormTaskMapper.getSelectContractList(pageRequest);
    }
}
